package com.smartcruise.main.mainFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.smartcruise.R;
import com.smartcruise.base.BaseFragment;
import com.smartcruise.base.FragmentActivitySimpleImp;
import com.smartcruise.base.RefreshContract;
import com.smartcruise.base.RefreshSimpleImp;
import com.smartcruise.main.mainFragment.messageNotice.MessageNoticeFragment;
import com.smartcruise.main.mainFragment.tools.CompassFragment;
import com.smartcruise.main.mainFragment.tools.SosFragment;
import com.smartcruise.main.mainFragment.tools.globalSearch.GlobalSearchFragment;
import com.smartcruise.main.mainFragment.tools.map.MapActivity;
import com.smartcruise.report.EditType;
import com.smartcruise.report.ReportDataEditActivity;
import com.smartcruise.task.completedTask.CompletedTaskFragment;
import com.smartcruise.task.newTask.NewTaskFragment;
import com.smartcruise.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 * 首页
 */

public class MainFragment extends BaseFragment implements MainContract.View {
    @BindView(R.id.taskSearchLay)
    LinearLayout taskSearchLay;
    @BindView(R.id.newMessageCount)
    TextView newMessageCount;
    @BindView(R.id.newMessageLay)
    FrameLayout newMessageLay;
    @BindView(R.id.reportLay)
    LinearLayout reportLay;
    @BindView(R.id.sosLay)
    LinearLayout sosLay;
    @BindView(R.id.compassLay)
    LinearLayout compassLay;
    @BindView(R.id.mapLay)
    LinearLayout mapLay;
    @BindView(R.id.newTaskNotice)
    TextView newTaskNotice;
    @BindView(R.id.newTaskLay)
    FrameLayout newTaskLay;
    @BindView(R.id.rightTaskNotice)
    TextView rightTaskNotice;
    @BindView(R.id.completedTaskLay)
    FrameLayout completedTaskLay;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.emptyView)
    TextView emptyView;
    Unbinder unbinder;
    @BindView(R.id.swipeRefreshLayout)
    SmartRefreshLayout swipeRefreshLayout;
    @BindView(R.id.completedTaskCount)
    TextView completedTaskCount;

    private RefreshContract.Presenter mPresenter;
    protected RefreshContract.RefreshInterface refreshInterface;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getRootView(R.layout.fragment_main_page);
        setEnableRightSlide(false);
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        mPresenter.destroy();
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {
        refreshInterface = new RefreshSimpleImp(swipeRefreshLayout);
        refreshInterface.setOnRefreshListener(new RefreshContract.OnRefreshListener() {
            @Override
            public void onRefresh(RefreshContract.RefreshInterface refreshInterface) {
                mPresenter.refresh();
            }
        });
        refreshInterface.setOnLoadMoreListener(new RefreshContract.OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshContract.RefreshInterface refreshInterface) {
                mPresenter.loadMore();
            }
        });
        mPresenter = new MainPresenterImp(this);
        mPresenter.start();

//        View noMore = getLayoutInflater().inflate(R.layout.textview_no_more, null);
//        listView.addFooterView(noMore);
        listView.setEmptyView(emptyView);
        listView.setPadding(0, Utils.px(16), 0, Utils.px(16));
        listView.setDividerHeight(Utils.px(8));
    }

    @OnClick(R.id.taskSearchLay)
    public void onTaskSearchLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), GlobalSearchFragment.class);
    }

    @OnClick(R.id.newMessageLay)
    public void onNewMessageLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), MessageNoticeFragment.class);
    }

    @OnClick(R.id.reportLay)
    public void onReportLayClicked() {
        ReportDataEditActivity.start(getActivity(), EditType.CREATE, null);
    }

    @OnClick(R.id.sosLay)
    public void onSosLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), SosFragment.class);
    }

    @OnClick(R.id.compassLay)
    public void onCompassLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), CompassFragment.class);
    }

    @OnClick(R.id.newTaskLay)
    public void onNewTaskLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), NewTaskFragment.class);
    }

    @OnClick(R.id.completedTaskLay)
    public void onCompletedTaskLayClicked() {
        FragmentActivitySimpleImp.start(getContext(), CompletedTaskFragment.class);
    }

    @OnClick(R.id.mapLay)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), MapActivity.class));
    }

    @Override
    public void showNoticeCount(String count) {
        if (TextUtils.isEmpty(count)) {
            newMessageCount.setVisibility(View.GONE);
        } else {
            newMessageCount.setVisibility(View.VISIBLE);
            newMessageCount.setText(count);
        }
    }

    @Override
    public void showNewTaskNoticeCount(int count) {
        if (count <= 0) {
            newTaskNotice.setVisibility(View.GONE);
        } else {
            newTaskNotice.setVisibility(View.VISIBLE);
            newTaskNotice.setText(String.valueOf(count));
        }
    }

    /**
     * description:显示已完成任务数目 如果没有 则不显示
     *
     * @param count 已完成任务数目
     */
    @Override
    public void showCompletedTaskNoticeCount(int count) {
        if (count <= 0) {
            completedTaskCount.setVisibility(View.GONE);
        } else {
            completedTaskCount.setVisibility(View.VISIBLE);
            completedTaskCount.setText(String.valueOf(count));
        }
    }

    /**
     * description:获取刷新组件
     */
    @Override
    public RefreshContract.RefreshInterface getRefresh() {
        return refreshInterface;
    }

    /**
     * description:设置数据
     *
     * @param baseAdapter
     */
    @Override
    public void setData(BaseAdapter baseAdapter) {
        listView.setAdapter(baseAdapter);
    }
}
